package com.ezjie.ielts.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

/**
 * 用讯飞语音的tts读单词和句子
 * 
 * @author renyangyang
 *
 */
public class SpeechTTSUtil implements SynthesizerListener {

	private Context mContext;
	private AnimationDrawable animation;
	// 语音合成对象
	private SpeechSynthesizer mTts;
	// 默认云端发音人
	public static String voicerCloud = "vimary";
	private SharedPreferences mSharedPreferences;
	private static SpeechTTSUtil speechTTSUtil;

	private SpeechTTSUtil(Context context) {
		this.mContext = context;
		init();
	}

	public static SpeechTTSUtil getInstance(Context context) {
		if (speechTTSUtil == null) {
			speechTTSUtil = new SpeechTTSUtil(context);

		}
		return speechTTSUtil;
	}

	public void init() {
		// 初始化合成对象
		mTts = SpeechSynthesizer.createSynthesizer(mContext, mTtsInitListener);
		mSharedPreferences = mContext.getSharedPreferences(TtsSettings.PREFER_NAME, Activity.MODE_PRIVATE);
	}

	public void speak(String word, AnimationDrawable animation) {
		this.animation = animation;
		// 设置参数
		setParam();
		int code = mTts.startSpeaking(word, this);
		if (code != ErrorCode.SUCCESS) {
			AppUtil.showToast(mContext, "语音合成失败,错误码: " + code);
		}
	}

	public void speak(String word) {
		// this.animation = animation;
		// 设置参数
		setParam();
		int code = mTts.startSpeaking(word, this);
		if (code != ErrorCode.SUCCESS) {
			AppUtil.showToast(mContext, "语音合成失败,错误码: " + code);
		}
	}

	public void close() {
		if (speechTTSUtil != null) {
			speechTTSUtil = null;
		}
	}

	public void pause() {
		if (mTts != null) {
			mTts.stopSpeaking();
			// 退出时释放连接
			mTts.destroy();
		}
		if (animation != null) {
			animation.selectDrawable(0);
			animation.stop();
		}
	}

	/**
	 * 参数设置
	 * 
	 * @param param
	 * @return
	 */
	private void setParam() {
		// 清空参数
		mTts.setParameter(SpeechConstant.PARAMS, null);
		// 设置合成
		// 设置使用云端引擎
		mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
		// 设置发音人
		mTts.setParameter(SpeechConstant.VOICE_NAME, voicerCloud);

		// 设置语速
		mTts.setParameter(SpeechConstant.SPEED, mSharedPreferences.getString("speed_preference", "35"));

		// 设置音调
		mTts.setParameter(SpeechConstant.PITCH, mSharedPreferences.getString("pitch_preference", "50"));

		// 设置音量
		mTts.setParameter(SpeechConstant.VOLUME, mSharedPreferences.getString("volume_preference", "50"));

		// 设置播放器音频流类型
		mTts.setParameter(SpeechConstant.STREAM_TYPE, mSharedPreferences.getString("stream_preference", "3"));
	}

	/**
	 * 初始化监听。
	 */
	private InitListener mTtsInitListener = new InitListener() {
		@Override
		public void onInit(int code) {
			LogUtils.d("InitListener init() code = " + code);
			if (code != ErrorCode.SUCCESS) {
				AppUtil.showToast(mContext, "初始化失败,错误码：" + code);
			}
		}
	};

	@Override
	public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCompleted(SpeechError arg0) {
		// TODO Auto-generated method stub
		if (animation != null) {
			animation.selectDrawable(0);
			animation.stop();
		}
	}

	@Override
	public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSpeakBegin() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSpeakPaused() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSpeakProgress(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSpeakResumed() {
		// TODO Auto-generated method stub

	}
}
